//
//  ByFootBallMixedPopView.m
//  caipiao_ios
//
//  Created by mc on 2018/7/21.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByFootBallMixedPopView.h"
#import "BySureCancelView.h"


#define ViewWidth  (SCREEN_WIDTH - 20)
#define WIDTH (ViewWidth - 20)
@interface ByFootBallMixedPopView ()


@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIScrollView *scrollView;


@property(nonatomic,strong)UIView * spfView;//胜平负

@property(nonatomic,strong)UIView * scoresView;//比分

@property(nonatomic,strong)UIView * allGoalsView;//总进球

@property(nonatomic,strong)UIView * halfAllView;//半全场

@property(nonatomic,strong) NSMutableArray * spfArray;
@property(nonatomic,strong) NSMutableArray * scoresArray;
@property(nonatomic,strong) NSMutableArray * goalsArray;
@property(nonatomic,strong) NSMutableArray * halfAllArray;

@property(nonatomic,strong) NSMutableArray * spfArray1;
@property(nonatomic,strong) NSMutableArray * scoresArray1;
@property(nonatomic,strong) NSMutableArray * goalsArray1;
@property(nonatomic,strong) NSMutableArray * halfAllArray1;



@end


@implementation ByFootBallMixedPopView

- (id)init{
    self = [super init];
    if (self) {
        UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backgroundView.backgroundColor = rgba(120, 120, 120, 0.3);
        self.backgroundView = backgroundView;
        [[UIApplication sharedApplication].keyWindow addSubview:backgroundView];
        
        
        
        self.frame = CGRectMake(10, 60, ViewWidth, SCREEN_HEIGHT - 60 - 49);
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5;
        self.backgroundColor = BY_BACKGROUND_COLOR;
        
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, ViewWidth, self.frame.size.height - 50 - 60)];
        [self addSubview:self.scrollView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ViewWidth, 30)];
        [self.scrollView addSubview:titleLabel];
        titleLabel.text = @"胜平负、让球胜负不支持单关";
        titleLabel.font = FONT(16);
        titleLabel.textColor = BY_THEME_COLOR;;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        
        self.scrollView.contentSize = CGSizeMake(ViewWidth, self.scrollView.size.height);
        
        
        
        self.spfView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, WIDTH, 83)];
        [self.scrollView addSubview:self.spfView];
        self.spfView.backgroundColor = BY_LINE_COLOR;
        self.spfView.layer.cornerRadius = 5;
        self.spfView.layer.masksToBounds = YES;
        
        self.halfAllView = [[UIView alloc] initWithFrame:CGRectMake(10, self.spfView.bottom + 20, WIDTH, 93)];
        [self.scrollView addSubview:self.halfAllView];
        self.halfAllView.backgroundColor = BY_LINE_COLOR;
        self.halfAllView.layer.cornerRadius = 5;
        self.halfAllView.layer.masksToBounds = YES;
        
        
        self.allGoalsView = [[UIView alloc] initWithFrame:CGRectMake(10, self.halfAllView.bottom + 20, WIDTH, 93)];
        [self.scrollView addSubview:self.allGoalsView];
        self.allGoalsView.backgroundColor = BY_LINE_COLOR;
        self.allGoalsView.layer.cornerRadius = 5;
        self.allGoalsView.layer.masksToBounds = YES;
        
        
        self.scoresView = [[UIView alloc] initWithFrame:CGRectMake(10, self.allGoalsView.bottom + 20, WIDTH, 46 * 7)];
        [self.scrollView addSubview:self.scoresView];
        self.scoresView.backgroundColor = BY_LINE_COLOR;
        self.scoresView.layer.cornerRadius = 5;
        self.scoresView.layer.masksToBounds = YES;
        self.scrollView.contentSize = CGSizeMake(ViewWidth, self.scoresView.bottom);
        
        
        BySureCancelView *sureCancelView = [[BySureCancelView alloc] init];
        [self addSubview:sureCancelView];
        [sureCancelView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(0);
            make.right.equalTo(self).offset(0);
            make.bottom.equalTo(self).offset(0);
            make.height.mas_equalTo(@60);
        }];
        sureCancelView.cancelBlock = ^(){
            [self removeViews];
            
            if (self.selectedBlock) {
                self.selectedBlock(self.spfArray1 ,self.halfAllArray1, self.goalsArray1 , self.scoresArray1);
            }
        };
        sureCancelView.sureBlock = ^(){
            
            if (self.selectedBlock) {
                self.selectedBlock(self.spfArray ,self.halfAllArray, self.goalsArray , self.scoresArray);
            }
            
            [self removeViews];
        };
        [self initTitleViews];
        
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }
    return self;
}

- (void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
}


- (void)setDataSpfArray:(NSMutableArray *)spfArray AndHalfAllArray:(NSMutableArray *)halfAllArray AndGoalsArray:(NSMutableArray *)goalsArray AndScoresArray:(NSMutableArray *)scoresArray{
    
    
    self.spfArray1 = [NSMutableArray arrayWithCapacity:1];
    self.halfAllArray1 = [NSMutableArray arrayWithCapacity:1];
    self.goalsArray1 = [NSMutableArray arrayWithCapacity:1];
    self.scoresArray1 = [NSMutableArray arrayWithCapacity:1];
    
    ByNetModel *model = [spfArray firstObject];
    self.bottomView.netModel = model;
    
    for (ByNetModel *netModel in spfArray) {
        ByNetModel *model = [netModel copy];
        [self.spfArray1 addObject:model];
    }
    
    for (ByNetModel *netModel in halfAllArray) {
        ByNetModel *model = [netModel copy];
        [self.halfAllArray1 addObject:model];
    }
    
    for (ByNetModel *netModel in goalsArray) {
        ByNetModel *model = [netModel copy];
        [self.goalsArray1 addObject:model];
    }
    
    for (ByNetModel *netModel in scoresArray) {
        ByNetModel *model = [netModel copy];
        [self.scoresArray1 addObject:model];
    }
    
    self.spfArray = spfArray;
    self.halfAllArray = halfAllArray;
    self.goalsArray = goalsArray;
    self.scoresArray = scoresArray;
    

    [self initSpfViews];
    [self initHalfAllViews];
    [self initGoalsViews];
    [self initPointsViews];
}


- (void)initTitleViews{
    ByTootBallPopTitleView *bottomView = [[ByTootBallPopTitleView alloc] init];
    [self addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(0);
        make.top.equalTo(self).offset(0);
        make.right.equalTo(self).offset(0);
        make.height.mas_equalTo(@50);;
    }];
    self.bottomView = bottomView ;
}

- (void)initSpfViews{
    
   [self.spfView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UILabel *pointLabel1 = [self createLabelWithFrame:CGRectMake(1, 1, 20, 40) backgroundColor:rgba(116, 180, 226, 1) text:@"0" textColor:[UIColor whiteColor] font:12];
    [self.spfView addSubview:pointLabel1];
    
    UILabel *pointLabel2 = [self createLabelWithFrame:CGRectMake(1, 42, 20, 40) backgroundColor:rgba(193, 225, 95, 1) text:@"-1" textColor:BY_TEXT_COLOR font:12];
    [self.spfView addSubview:pointLabel2];
    
    ByNetModel *pointModel = [self.spfArray objectAtIndex:3];
    pointLabel2.text = pointModel.fixedodds;
  

    CGFloat left = 1;
    CGFloat magin = 1;
    CGFloat width = (WIDTH - 21 - left * 4) / 3.0;
    CGFloat height = (83 - left * 3) / 2.0;
    
    for (int i = 0;i < self.spfArray.count; i++) {
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(22 + i % 3 * (width + magin),1 + i / 3 * (height + magin), width,height)];
        [self.spfView addSubview:valueLabel];
        valueLabel.tag = 100 + i;
        valueLabel.font = FONT(14);
        valueLabel.textAlignment = NSTextAlignmentCenter;
       
        ByNetModel *netModel = [self.spfArray objectAtIndex:i];
       
        if (netModel.selected) {
            valueLabel.backgroundColor = rgba(255, 148, 87, 1);
            valueLabel.textColor = [UIColor whiteColor];
        }else{
            
            valueLabel.textColor = [UIColor lightGrayColor];
            valueLabel.backgroundColor = [UIColor whiteColor];
            
        }
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(spfTapAction:)];
        [valueLabel addGestureRecognizer:tap];
        
        
        if (netModel.isNoSale) {
            valueLabel.userInteractionEnabled = NO;
            valueLabel.text = @"未开售";
        }else{
            valueLabel.userInteractionEnabled = YES;
            valueLabel.text = [NSString stringWithFormat:@"%@ %@", netModel.title,netModel.valueString];
        }
    }
}

- (void)spfTapAction:(UITapGestureRecognizer *)tap{
    
    
    
    
    NSInteger index = tap.view.tag - 100;
//    if (index < 3) {
//        BOOL isClick = [ByFootBallHandle isOrNotClickWithRowSpfData:self.spfArray goalsData:self.goalsArray halfAllData:self.halfAllArray scoresData:self.scoresArray WithType:1];
//        if (!isClick) {
//            return ;
//        }
//    }else{
//        BOOL isClick = [ByFootBallHandle isOrNotClickWithRowSpfData:self.spfArray goalsData:self.goalsArray halfAllData:self.halfAllArray scoresData:self.scoresArray WithType:2];
//        if (!isClick) {
//            return ;
//        }
//    }
    
    
    
    ByNetModel *netModel = [self.spfArray objectAtIndex:index];
    NSLog(@"胜平负 single = %@", netModel.single);
    netModel.selected = !netModel.selected;
    [self initSpfViews];
}

- (void)initHalfAllViews{
    
   [self.halfAllView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UILabel *pointLabel1 = [self createLabelWithFrame:CGRectMake(1, 1, 20, 91) backgroundColor:rgba(191, 237, 190, 1) text:@"半\n全\n场" textColor:[UIColor whiteColor] font:12];
    pointLabel1.numberOfLines = 0;
    [self.halfAllView addSubview:pointLabel1];
    
    CGFloat left = 1;
    CGFloat magin = 1;
    CGFloat width = (WIDTH - 21 - left * 6) / 5.0;
    CGFloat height = (93 - left * 3) / 2.0;
    
    for (int i = 0;i < self.halfAllArray.count; i++) {
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(22 + i % 4 * (width + magin),1 + i / 4 * (height + magin), width,height)];
        [self.halfAllView addSubview:valueLabel];
        valueLabel.tag = 200 + i;
        valueLabel.font = FONT(12);
        valueLabel.numberOfLines = 0;
        valueLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(halfAllTapAction:)];
        [valueLabel addGestureRecognizer:tap];
        
        if (i == self.halfAllArray.count - 1) {
            [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.halfAllView).offset(-1);
                make.top.equalTo(self.halfAllView).offset(1);
                make.bottom.equalTo(self.halfAllView).offset(-1);
                make.width.mas_equalTo(@(width));
            }];
        }
        
        
        ByNetModel *netModel = [self.halfAllArray objectAtIndex:i];
        
        
        if (netModel.isNoSale) {
            valueLabel.userInteractionEnabled = NO;
            valueLabel.text = @"未开售";
        }else{
            valueLabel.userInteractionEnabled = YES;
            valueLabel.text = [NSString stringWithFormat:@"%@\n%@", netModel.title,netModel.valueString];
        }
        if (netModel.selected) {
            valueLabel.backgroundColor = rgba(255, 148, 87, 1);
            valueLabel.textColor = [UIColor whiteColor];
        }else{
            
            valueLabel.textColor = [UIColor lightGrayColor];
            valueLabel.backgroundColor = [UIColor whiteColor];
            
        }
    }
    
    
}

- (void)halfAllTapAction:(UITapGestureRecognizer *)tap{
//    BOOL isClick = [ByFootBallHandle isOrNotClickWithRowSpfData:self.spfArray goalsData:self.goalsArray halfAllData:self.halfAllArray scoresData:self.scoresArray WithType:5];
//    if (!isClick) {
//        return ;
//    }
    NSInteger index = tap.view.tag - 200;
    ByNetModel *netModel = [self.halfAllArray objectAtIndex:index];
    NSLog(@"半全场 single = %@", netModel.single);
    netModel.selected = !netModel.selected;
    [self initHalfAllViews];
    
}


- (void)initGoalsViews{
    
   [self.allGoalsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UILabel *pointLabel1 = [self createLabelWithFrame:CGRectMake(1, 1, 20, 91) backgroundColor:rgba(254, 199, 115, 1) text:@"总\n进\n球" textColor:BY_TEXT_COLOR font:12];
    pointLabel1.numberOfLines = 0;
    [self.allGoalsView addSubview:pointLabel1];
    
    
    CGFloat left = 1;
    CGFloat magin = 1;
    CGFloat width = (WIDTH - 21 - left * 5) / 4.0;
    CGFloat height = (93 - left * 3) / 2.0;
    
    for (int i = 0;i < self.goalsArray.count; i++) {
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(22 + i % 4 * (width + magin),1 + i / 4 * (height + magin), width,height)];
        [self.allGoalsView addSubview:valueLabel];
        valueLabel.tag = 300 + i;
        valueLabel.font = FONT(12);
        valueLabel.numberOfLines = 0;
        valueLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(allGoalsTapAction:)];
        [valueLabel addGestureRecognizer:tap];
        
        
        ByNetModel *netModel = [self.goalsArray objectAtIndex:i];
       
        if (netModel.isNoSale) {
            valueLabel.userInteractionEnabled = NO;
            valueLabel.text = @"未开售";
        }else{
            valueLabel.userInteractionEnabled = YES;
             valueLabel.text = [NSString stringWithFormat:@"%@球\n%@", netModel.title,netModel.valueString];
        }
        
        
        if (netModel.selected) {
            valueLabel.backgroundColor = rgba(255, 148, 87, 1);
            valueLabel.textColor = [UIColor whiteColor];
        }else{
            
            valueLabel.textColor = [UIColor lightGrayColor];
            valueLabel.backgroundColor = [UIColor whiteColor];
            
        }

    }
    
}

- (void)allGoalsTapAction:(UITapGestureRecognizer *)tap{
//    BOOL isClick = [ByFootBallHandle isOrNotClickWithRowSpfData:self.spfArray goalsData:self.goalsArray halfAllData:self.halfAllArray scoresData:self.scoresArray WithType:3];
//    if (!isClick) {
//        return ;
//    }
    
    NSInteger index = tap.view.tag - 300;
    ByNetModel *netModel = [self.goalsArray objectAtIndex:index];
    NSLog(@"总进球 single = %@", netModel.single);
    netModel.selected = !netModel.selected;
    [self initGoalsViews];
    
}

- (void)initPointsViews{
   [self.scoresView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UILabel *pointLabel1 = [self createLabelWithFrame:CGRectMake(1, 1, 20, 46 * 7 - 2) backgroundColor:rgba(244, 183, 255, 1) text:@"比\n分" textColor:BY_TEXT_COLOR font:12];
    pointLabel1.numberOfLines = 0;
    [self.scoresView addSubview:pointLabel1];
    
    
    CGFloat left = 1;
    CGFloat magin = 1;
    CGFloat width = (WIDTH - 21 - left * 6) / 5.0;
    CGFloat height = 45;
    for (int i = 0; i < 13; i++) {
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(22 + i % 5 * (width + magin),1 + i / 5 * (height + magin), width,height)];
        [self.scoresView addSubview:valueLabel];
        valueLabel.tag = 400 + i;
        valueLabel.font = FONT(12);
        valueLabel.textAlignment = NSTextAlignmentCenter;
        valueLabel.numberOfLines = 0;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scoresTapAction:)];
        [valueLabel addGestureRecognizer:tap];
        if (i == 12) {
            [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.scoresView).offset(-1);
                make.top.equalTo(self.scoresView).offset(3 + 45 * 2);
                make.size.mas_equalTo(CGSizeMake(width * 3 + 2, height));
            }];
        }
        
        
        ByNetModel *netModel = [self.scoresArray objectAtIndex:i];
        if (netModel.isNoSale) {
            valueLabel.userInteractionEnabled = NO;
            valueLabel.text = @"未开售";
        }else{
            valueLabel.userInteractionEnabled = YES;
            valueLabel.text = [NSString stringWithFormat:@"%@\n%@", netModel.title,netModel.valueString];
        }
        
        
        if (netModel.selected) {
            valueLabel.backgroundColor = rgba(255, 148, 87, 1);
            valueLabel.textColor = [UIColor whiteColor];
        }else{
            
            valueLabel.textColor = [UIColor lightGrayColor];
            valueLabel.backgroundColor = [UIColor whiteColor];
            
        }

    }
    
    
    for (int i = 0; i < self.scoresArray.count - 13; i++) {
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(22 + i % 5 * (width + magin),height * 3 + 4 + i / 5 * (height + magin), width,height)];
        [self.scoresView addSubview:valueLabel];
        valueLabel.tag = 400 + i + 13;
        valueLabel.font = FONT(12);
        valueLabel.numberOfLines = 0;
        valueLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scoresTapAction:)];
        [valueLabel addGestureRecognizer:tap];
        
        
        
        ByNetModel *netModel = [self.scoresArray objectAtIndex:i + 13];
        
        if (netModel.isNoSale) {
            valueLabel.userInteractionEnabled = NO;
            valueLabel.text = @"未开售";
        }else{
            valueLabel.userInteractionEnabled = YES;
            valueLabel.text = [NSString stringWithFormat:@"%@\n%@", netModel.title,netModel.valueString];
        }
        
        if (netModel.selected) {
            valueLabel.backgroundColor = rgba(255, 148, 87, 1);
            valueLabel.textColor = [UIColor whiteColor];
        }else{
            
            valueLabel.textColor = [UIColor lightGrayColor];
            valueLabel.backgroundColor = [UIColor whiteColor];
            
        }
       
    }
    
}

- (void)scoresTapAction:(UITapGestureRecognizer *)tap{
    
//    BOOL isClick = [ByFootBallHandle isOrNotClickWithRowSpfData:self.spfArray goalsData:self.goalsArray halfAllData:self.halfAllArray scoresData:self.scoresArray WithType:4];
//    if (!isClick) {
//        return ;
//    }
    
    NSInteger index = tap.view.tag - 400;
    ByNetModel *netModel = [self.scoresArray objectAtIndex:index];
    netModel.selected = !netModel.selected;
    [self initPointsViews];
   
}



- (void)removeViews{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(self.frame.origin.x-SCREEN_WIDTH, self.frame.origin.y,self.frame.size.width, self.frame.size.height);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.backgroundView removeFromSuperview];
        
        [self removeFromSuperview];
    });
}

- (UILabel *)createLabelWithFrame:(CGRect)frame backgroundColor:(UIColor *)backColor text:(NSString *)text textColor:(UIColor *)textColor font:(int)font{
    
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    if (backColor) {
        label.backgroundColor = backColor;
    }else{
        label.backgroundColor = [UIColor clearColor];
    }
    label.text = text;
    label.textColor = textColor;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

@end
